package com.xiangyi.teachingsystem.controller;

import com.xiangyi.teachingsystem.entity.po.GroupUser;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GroupUserController {
    @Autowired
    GroupUserService groupUserService;

    @PostMapping("/addUsersToGroup")
    public Result addUsersToGroup(@RequestBody List<GroupUser> groupUsers){
        try {
            groupUserService.addUsersToGroup(groupUsers);
            return Result.success("用户批量添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("参数错误").setData(e.getMessage());
        }
    }

    @PostMapping("/removeUsersFromGroup")
    public Result removeUsersFromGroup(@RequestBody List<Integer> ids){
        try {
            groupUserService.removeByIds(ids);
            return Result.success("用户批量移除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("groupUsersList")
    public Result groupUsersList(@RequestParam Integer groupId){
        try {
            List<GroupUser> groupUsers = groupUserService.groupUsersList(groupId);
            return Result.success("小组成员查询成功").setData(groupUsers);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

}
